local storyboard = require( "storyboard" )
local BackgroundBG = require( 'scripts.models.BackgroundBG' )
local scene = storyboard.newScene()
local totalDelta = 0

local introMusicChannel

function scene:updateLevelCallback()
   return function(delta)
      self.historyTxt.y = self.historyTxt.y - delta*14
      if(self.historyTxt.y < -Y*0.4) then
         LEVEL = 2
         storyboard.gotoScene("scripts.scenes.GridTestScene", {effect = "fade", time = 100})
      end
   end
end

function scene:createScene(event)
   self.gameController = GameController:new()
   local group=self.view
   logger:gameEvent("Create Intro Game Scene")
   storyboard.removeAll()
   self.timers = {}

   local trees = display.newImageRect( "resources/sprites/miscBG.jpg", X, Y)
   trees.x = X*0.5
   trees.y = Y*0.5
   group:insert(trees)
   local introText = 'The story starts in a forsaken kingdom.... There is a king whose greed extended beyond  his realm, He was leading his people into disaster, and justice was nothing more than a utopy the citizens could only dream of. But .... when everything seemed to be lost.... a brave peasant tired of  injustice emerged, and found a way to put an end to the King malefical acts. Therefore, a royal decree was emmited .... YOU ARE WANTED DEAD OR ALIVE!!!!!. You must escape the realm, but people will help you only if they relate to you..........'
   local historyTxt = display.newText({text=introText, x=X*0.5, y=Y, width=X*0.9, height=Y, font=Font.get('seagram'), fontSize=18} )
   self.historyTxt = historyTxt
   group:insert(historyTxt)


   local skipText = display.newText({text='SKIP   ',parent=self.view,x=X*0.85,y=Y*0.1,fontSize=20, font=Font.get('seagram')})
   skipText:setFillColor(1,0,0)
   DisplayObjectUtils.addTapEventListener(
      skipText,
      function()
         LEVEL = 2
         storyboard.gotoScene("scripts.scenes.GridTestScene", {effect = "fade", time = 100})
      end
   )
end

--Methods
function scene:enterScene(event)
   logger:gameEvent("Enter Test Scene")

   introMusicChannel = audio.play( introMusic, { channel=10, loops=-1, fadein=5000 }  )
   self.gameController:addCallback(scene:updateLevelCallback(), 'mainLoop')
   self.gameController:beginGame()
end

function scene:exitScene(event)
   logger:gameEvent("exit Test Scene")

   self.gameController:clearAllCallbacks()
   self.gameController:stop()
end

function scene:destroyScene(event)
   logger:gameEvent("destroy Test Scene")

   for i=#self.timers,1,-1 do
      timer.cancel(self.timers[i])
      table.remove(self.timers,i)
   end

   audio.stop( introMusicChannel )
   self.gameController:clearAllCallbacks()
   self.gameController:stop()
end

scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene", scene)

return scene

